<?php

function confirm_query($result_set) {
	if(!$result_set) {
		die("database query failed.");
	}
}

function find_all_subjects() {
	  global $connection;
 //2. perform database query
 
		  $query ="SELECT * FROM subjects "  ;
		 // $query .=" subjects where visible = 1";
		  $query .=" ORDER by position ASC";
		  $subject_set = mysqli_query($connection, $query);
		  confirm_query($subject_set);
		  $subject = mysqli_fetch_assoc($subject_set);
		  return $subject_set;	
	
	
}

function find_pages_for_subjects($subject_id) {
    global $connection;
			  $safe_subject_id = (mysqli_real_escape_string($connection, $subject_id));

	$query = "SELECT * from pages";
	$query .= " WHERE visible = 1"; 
	$query .= " AND subject_id = {$safe_subject_id}";
	$query .= " ORDER by position ASC";
    $page_set = mysqli_query($connection, $query);
    confirm_query($page_set);	
	return $page_set;
	
	
	
}

function find_subject_by_id($subject_id) {
	
	      global $connection;
         		  $safe_subject_id = (mysqli_real_escape_string($connection, $subject_id));
 //2. perform database query
 
		  $query ="SELECT * FROM"  ;
		  $query .=" subjects WHERE id={$safe_subject_id} ";
		  $query .=" LIMIT 1";
		  $safe_subject_id = mysqli_query($connection, $query);
		  confirm_query($subject_id);
		  if ($subject = mysqli_fetch_assoc($safe_subject_id )) {
			  return $subject;
		  } else {
			  return null;
		  }
}

function find_page_by_id($page_id) {
	global $connection;
   
   $safe_page_id = mysqli_real_escape_string($connection, $page_id);
   $query = "SELECT * ";
   $query .= "FROM pages ";
   $query .= "WHERE id = {$safe_page_id} ";
   $query .= "LIMIT 1";
   $page_set = mysqli_query($connection, $query);
   confirm_query($page_set);
   if($page = mysqli_fetch_assoc($page_set)) {
	   return $page;
   } else {
	   return null;
   }
	
	
	
	
	
	
}

 //navigatoion takes 2 arguements
 function navigation ($subject_id, $page_id) {
	 
	 $output = "<ul class=\"subjects\">";
     $subject_set = find_all_subjects();
	  // use returned data (if any)
	  while($subject = mysqli_fetch_assoc($subject_set)) {
		  //output data from each ro
      
     $output .= "<li";
	   if ($subject["id"] == $subject_id) {
	      $output .= " class=\"selected\"";
	   }
	   $output .=">";
	   $output .= "<a href=\"manage_content.php?subject=";
	   $output .= urlencode($subject["id"]);
	   $output .="\">";
	   $output .=$subject["menu_name"];
	   $output .="</a>";
	   
       $page_set = find_pages_for_subjects($subject["id"]);
      
       $output  .= "<ul class=\"pages\">";
      
        
	  while($page = mysqli_fetch_assoc($page_set)) {
		  //output data from each row
     
       $output  .= "<li";
	   if ($page["id"] == $page_id) {
	       $output  .= " class=\"selected\"";
	   }
	    $output  .=">";
		
        $output  .="<a href=\"manage_content.php?page=";
		    $output  .= urlencode($page["id"]);
			$output  .= "\">"; 
			$output .= $page["menu_name"]; 
			$output  .="</a></li>";
 
	    }
      	    mysqli_free_result($page_set);

         
      $output  .="</ul>";
      $output  .= "</li>";
      
	  }
	
       

	   // 4.  Release returned data
	   mysqli_free_result($subject_set);
	   
	  
     $output  .="</ul>";
	 return $output;
	 
	 
	 
 }
 
?>
